using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Test1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Sprite TextHelicopter;
        Sprite TextBackground;
        Sprite TextBase;
        Sprite TextBullet;
        Sprite TextCanon;

        const double VELOCIDAD = 5f;
        double velX = 0;
        double velY = 0;
        double velHeli = -3;

        KeyboardState ks;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            TextHelicopter = new Sprite();
            TextBackground = new Sprite();
            TextBase = new Sprite();
            TextBullet = new Sprite();
            TextCanon = new Sprite();


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            TextBackground.Texture = Content.Load<Texture2D>("Background");
            TextBase.Texture = Content.Load<Texture2D>("Base");
            TextHelicopter.Texture = Content.Load<Texture2D>("Helicopter");
            TextCanon.Texture = Content.Load<Texture2D>("Canon");
            TextBullet.Texture = Content.Load<Texture2D>("Bullet");

            TextBase.PosX = TextBackground.Texture.Width / 2 - TextBase.Texture.Width / 2;
            TextBase.PosY = TextBackground.Texture.Height - TextBase.Texture.Height;

            TextCanon.PosX = TextBase.PosX + TextBase.Texture.Width / 2 - TextCanon.Texture.Width / 2;
            TextCanon.PosY = TextBase.PosY - TextCanon.Texture.Height;

            TextBullet.PosX = TextBase.PosX + TextBase.Texture.Width / 2 - TextBullet.Texture.Width / 2;
            TextBullet.PosY = TextBase.PosY - TextBullet.Texture.Height;

            TextHelicopter.PosX = 800 + TextHelicopter.Texture.Width;
            TextHelicopter.PosY = 25;

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            ks = Keyboard.GetState(PlayerIndex.One);
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (ks.IsKeyDown(Keys.Left))
            {
                TextCanon.Angle-=0.1f;
            }

            if (ks.IsKeyDown(Keys.Right))
            {
                TextCanon.Angle += 0.1f;
            }

            if (ks.IsKeyDown(Keys.Space))
            {
                TextBullet.PosX = TextBase.PosX + TextBase.Texture.Width / 2 - TextBullet.Texture.Width / 2;
                TextBullet.PosY = TextBase.PosY - TextBullet.Texture.Height;
                velX = (float)(Math.Sin(TextCanon.Angle) * VELOCIDAD);
                velY = (float)(Math.Cos(TextCanon.Angle) * VELOCIDAD);
            }

            TextBullet.PosX += (float)(velX);
            TextBullet.PosY -= (float)(velY);

            if (TextHelicopter.PosX < 0 - TextHelicopter.Texture.Width - 10)
            {
                TextHelicopter.PosX = 0 - TextHelicopter.Texture.Width;
                TextHelicopter.Rotation = SpriteEffects.FlipHorizontally;
                velHeli *= -1;
            }
            else if (TextHelicopter.PosX > 800 + TextHelicopter.Texture.Width + 10)
            {
                TextHelicopter.PosX = 800 + TextHelicopter.Texture.Width;
                TextHelicopter.Rotation = SpriteEffects.None;
                velHeli *= -1;
            }
            else
            {
                TextHelicopter.PosX += (float)(velHeli);
            }


            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            spriteBatch.Draw(TextBackground.Texture, new Vector2(TextBackground.PosX, TextBackground.PosY), Color.White);
            spriteBatch.Draw(TextBase.Texture, new Vector2(TextBase.PosX, TextBase.PosY), Color.White);
            spriteBatch.Draw(TextBullet.Texture, new Vector2(TextBullet.PosX, TextBullet.PosY), Color.White);
            spriteBatch.Draw(TextCanon.Texture, new Vector2(TextCanon.PosX + TextCanon.Texture.Width / 2, TextCanon.PosY + TextCanon.Texture.Height), null, Color.White, (float)(TextCanon.Angle), new Vector2(TextCanon.Texture.Width / 2, TextCanon.Texture.Height), 1, SpriteEffects.None, 0);
            spriteBatch.Draw(TextHelicopter.Texture, new Vector2(TextHelicopter.PosX, TextHelicopter.PosY), null, Color.White, 0, new Vector2(0, 0), 1, TextHelicopter.Rotation, 0);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
